package cn.snailhd.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImgUtils {
	
	public static boolean resizeImage(String srcImgPath,
									String distImgPath,
									int width,int height){
		File srcFile = new File(srcImgPath);
		File distFile = new File(distImgPath);
		if(!srcFile.exists()){
			return false;
		}
		if(!distFile.getParentFile().exists()){
			distFile.getParentFile().mkdirs();
		}		
		try {
			Image srcImg = ImageIO.read(srcFile);
			BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			buffImg.getGraphics().drawImage(
					srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH),
					0,0,null);
			ImageIO.write(buffImg, "JPEG", new File(distImgPath));
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;	
	}

}
